package com.cskaoyan.service.impl;

import com.cskaoyan.bean.CskaoyanmallUser;
import com.cskaoyan.bean.CskaoyanmallUserExample;
import com.cskaoyan.bean.bo.WxAuthRegisterBo;
import com.cskaoyan.bean.bo.WxAuthResetBo;
import com.cskaoyan.mapper.CskaoyanmallUserMapper;
import com.cskaoyan.service.WXAuthService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class WXAuthServiceImpl implements WXAuthService {

    @Autowired
    CskaoyanmallUserMapper userMapper;


    @Override
    public CskaoyanmallUser selectUserByName(String username) {
        if (ObjectUtils.isEmpty(username)) {
            return null;
        }
        CskaoyanmallUserExample example = new CskaoyanmallUserExample();
        CskaoyanmallUserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<CskaoyanmallUser> users = userMapper.selectByExample(example);
        if (ObjectUtils.isEmpty(users)) {
            return null;
        }
        return users.get(0);
    }

    @Override
    public void updateUser(CskaoyanmallUser user) {
        if (!ObjectUtils.isEmpty(user) && !ObjectUtils.isEmpty(user.getId())) {
            userMapper.updateByPrimaryKey(user);
        }
    }

    @Override
    public int insertUser(WxAuthRegisterBo bo, HttpServletRequest req) {
        if (ObjectUtils.isEmpty(bo.getUsername()) || ObjectUtils.isEmpty(bo.getPassword()) || ObjectUtils.isEmpty(bo.getMobile())) {
            return 0;
        }
        CskaoyanmallUser user = new CskaoyanmallUser();
        user.setUsername(bo.getUsername());
        Md5Hash md5Hash = new Md5Hash(bo.getPassword());
        md5Hash.setIterations(1024);
        user.setPassword(md5Hash.toHex());
        user.setMobile(bo.getMobile());
        user.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif?imageView2/1/w/80/h/80");
        user.setAddTime(new Date());
        user.setUserLevel((byte) 0);
        user.setDeleted(false);
        user.setStatus((byte) 0);
        user.setGender((byte) 0);
        user.setWeixinOpenid(bo.getWxCode());
        user.setLastLoginIp(req.getRemoteAddr());
        user.setLastLoginTime(new Date());
        String nickName = "测试用户" + UUID.randomUUID().toString().substring(0, 5);
        user.setNickname(nickName);
        int insert = userMapper.insert(user);
        return insert;
    }

    @Override
    public int updatePassword(WxAuthResetBo bo) {
        if (ObjectUtils.isEmpty(bo.getPassword()) || ObjectUtils.isEmpty(bo.getMobile())) {
            return 0;
        }
        CskaoyanmallUser user = userMapper.selectUserByMobile(bo.getMobile());
        if (ObjectUtils.isEmpty(user)) {
            return 0;
        }
        Md5Hash md5Hash = new Md5Hash(bo.getPassword());
        md5Hash.setIterations(1024);
        user.setPassword(md5Hash.toHex());
        int i = userMapper.updateByPrimaryKey(user);
        return i;
    }

    @Override
    public String selectUsernameByMobile(String mobile) {
        CskaoyanmallUser user = userMapper.selectUserByMobile(mobile);
        if (ObjectUtils.isEmpty(user)) {
            return null;
        }
        return user.getUsername();
    }
}
